package com.orhon.smartcampus.modules.systemctl.fetcher;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.systemctl.dao.VersionManagementDao;
import com.orhon.smartcampus.modules.systemctl.entity.VersionManagement;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLVersionManagement;
import com.orhon.smartcampus.modules.systemctl.mm.VersionManagementMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class VersionManagementsFetcher implements DataFetcher {


    @Autowired
    VersionManagementDao versionManagementDao;

    @Autowired
    TokenService tokenService;

    /**
     * 获取版本列表
     *
     * @param environment
     * @return
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        List<GQLVersionManagement> gqlVersionManagements = new ArrayList<>();

        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }

        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(environment);
        String hql = "from VersionManagement where deleteAt = null ";

        if (environment.containsArgument("condition")) {
            HashMap<String, Object> map = ObjectToMap.to(environment.getArgument("condition"));
            if (map.get("id") != null) {
                hql += " and id =" + map.get("id");
            }
            if (map.get("title") != null) {
                hql += " and title " + Util.decide(map.get("title"));
            }
            if (map.get("remark") != null) {
                hql += " and remark " + Util.decide(map.get("remark"));
            }
            if (map.get("versionNumber") != null) {
                hql += " and versionNumber " + Util.decide(map.get("versionNumber"));
            }
            if (map.get("type") != null) {
                hql += " and type " + Util.decide(map.get("type"));
            }
            if (map.get("versionUrl") != null) {
                hql += " and versionUrl " + Util.decide(map.get("versionUrl"));
            }
            if (map.get("createTime") != null) {
                hql += " and createTime " + Util.decide(map.get("createTime"));
            }
            hql += " order by createTime desc";
        }
        List<VersionManagement> list = versionManagementDao.find(hql, null, page.getPage(), page.getLimit());

        for (VersionManagement item : list) {
            GQLVersionManagement versionManagement = VersionManagementMapper.MAPPER.toVersionManagement(item);
            gqlVersionManagements.add(versionManagement);
        }

        Long total = 0L;
        if (environment.getSelectionSet().contains("total")) {
            total = versionManagementDao.getTotalCount(hql);
        }
        return Result.ok().put("data", gqlVersionManagements).put("total", total);
    }
}
